import { useEffect, useState } from 'react'


//模拟一个接口请求
async function queryData(){
  const data =await new Promise((resolve) => {
    setTimeout(() => {
      resolve(666)
    }, 2000);
  })
  return data
}
function App() {
  let [num, setNum] = useState(1) //[1,func] setNum 取缓存值
  const [age,setAge] =useState(18)
  useEffect(()=>{
    console.log('useEffect');
    let timer =setInterval(() => {
        console.log(num);        
    }, 1000);
    return ()=>{
      console.log('组件卸载');
      clearInterval(timer)
    }
  },[num])
  function handle(pre){
    setNum(pre+1) //将 num 修改为 参数值，并触发视图更新
  }
  return (
    <div>
        <button onClick={handle}>{num}</button>
        <h2 onClick={()=> setAge(age+1)}>{age}</h2>
    </div>
  )
}

export default App;